package basic.study.wantOffer.chapter3;

import basic.study.wantOffer.chapter3.Problem18_.ListNode;

/**
 * @author Kevin
 * @date 2020/5/4 11:13
 * 反转链表
 */
public class Problem24 {
    public ListNode reverseList(ListNode head) {

        ListNode pre = null,cur = head,next;
        while (cur != null) {
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    //recursively
    public ListNode reverseList1(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
}
